package com.is.control;

import java.sql.ResultSet;

import com.is.dbconn.DBConnect;
import com.is.model.Evaluation;

public class EvaluationControl {
	
	public static boolean insertEvaluation(Evaluation e){
		DBConnect dbConnect = new DBConnect();
		String sql = "insert into evaluation (stuId,level,score,comments,records,stuyear) values(?,?,?,?,?,?)";
		dbConnect.prepareStatement(sql);
		try {
			setValue(e, dbConnect);
			if(dbConnect.executeUpdate()){
				return true;
			}
		} catch (Exception ex) {
			// TODO: handle exception
			ex.printStackTrace();
			System.out.println("insertEvaluation Error");
		}finally{
			dbConnect.close();
		}
		return false;
	}
	
	public static Evaluation getEvaluationByStuyearAndStuId(int stuId,int stuyear){
		DBConnect dbConnect = new DBConnect();
		String sql = "select *  from evaluation where stuId = '"+stuId+"' and stuyear = '"+stuyear+"'";
		ResultSet rs = null;
		dbConnect.prepareStatement(sql);
		try {
			rs = dbConnect.executeQuery();
			if(rs.next()){
				Evaluation evaluation = new Evaluation();
				getRs(evaluation, rs);
				return evaluation;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("getEvaluationByStuyearAndStuId Error");
		}finally{
			dbConnect.close();
		}
		return null;
	}
	
	public static Evaluation getEvaluationById(int id){
		DBConnect dbConnect = new DBConnect();
		String sql = "select *  from evaluation where id='"+id+"'";
		ResultSet rs = null;
		dbConnect.prepareStatement(sql);
		try {
			rs = dbConnect.executeQuery();
			if(rs.next()){
				Evaluation evaluation = new Evaluation();
				getRs(evaluation, rs);
				return evaluation;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("getEvaluationById Error");
		}finally{
			dbConnect.close();
		}
		return null;
	}
	
	public  static boolean updateEvaluation(Evaluation evaluation) {
		DBConnect dbConnect = new DBConnect();
		String sql = "update evaluation set stuId=?,level=?,score=?,comments=?,records=?,stuyear=? where id = '"+evaluation.getId()+"'";
		dbConnect.prepareStatement(sql);
		try {
			setValue(evaluation, dbConnect);
			if(dbConnect.executeUpdate()){
				return true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("updateEvaluation ERROR");
		}
		finally{
			dbConnect.close();
		}
		return false;
	}
	
	public static void setValue(Evaluation e,DBConnect dbc){
		//调用此方法需注意字段顺序(与sql语句保持一致)
		int index=1;
		try{
		dbc.setInt(index++, e.getStuId());
		dbc.setString(index++, e.getLevel());
		dbc.setString(index++, e.getScore());
		dbc.setString(index++, e.getComments());
		dbc.setString(index++, e.getRecords());
		dbc.setInt(index++, e.getStuyear());
	}catch (Exception ex) {
		// TODO: handle exception
		ex.printStackTrace();
		System.out.println("setValue方法出错");
	}
	}
	
	public static void getRs(Evaluation e ,ResultSet rs){
		try {
			e.setId(rs.getInt("id"));
			e.setComments(rs.getString("comments"));
			e.setLevel(rs.getString("level"));
			e.setRecords(rs.getString("records"));
			e.setScore(rs.getString("score"));
			e.setStuId(rs.getInt("stuId"));
			e.setStuyear(rs.getInt("stuyear"));
			
		} catch (Exception ex) {
			// TODO: handle exception
			ex.printStackTrace();
			System.out.println("getRs出错");
		}
	}

}
